import 'package:dio/dio.dart';
import 'package:image_picker/image_picker.dart';

import 'component/dialog/bottom.dart';
import 'httputils/httpmanager.dart';
import 'httputils/resultdata.dart';
import 'httputils/urlpath.dart';

class UploadMan {
  Future<void> picker(
      {int max = 1, required Function(List<XFile>) result}) async {
    showActionSheet(
      titles: ['相机', '相册'],
      onTap: (e) async {
        var index = e.first;
        if (index < 2) {
          if (max == 1 || index == 0) {
            var file = await ImagePicker().pickImage(
                source: index == 0 ? ImageSource.camera : ImageSource.gallery,
                maxHeight: 667,
                maxWidth: 375,
                imageQuality: 50);
            if (file == null) {
              result([]);
            } else {
              result([file]);
            }
          } else {
            var _files = await ImagePicker().pickMultiImage(
                maxHeight: 667, maxWidth: 375, imageQuality: 50);
            if (_files == null) {
              result([]);
            } else {
              result(_files);
            }
          }
        }
      },
    );
  }

  Future<List> _upload(List<XFile> files) async {
    if (files.isNotEmpty) {
      List<MultipartFile> _multipartFiles =
          files.map((e) => MultipartFile.fromFileSync(e.path)).toList();

      ResultData res = await HttpManager.getInstance().uploadFiles(
          UrlPath.postUploadFiles,
          multipartFiles: _multipartFiles);
      List<dynamic> list = res.data['data'];
      // return list.map((e) => FilePathEntity.fromJson(e)).toList();
    }
    return [];
  }
}
